home *** CD-ROM | disk | FTP | other *** search
- /*
- * LaunchInits application
- * v 1.0
- *
- * executes the code in each init passed in via the
- * OpenDocuments event
- *
- * June 1993 Greg Robbins
- * Developer Technical Support
- *
- */
-
- #include "AppleEvents.h"
- #include "GestaltEqu.h"
- #include "Resources.h"
-
- // prototypes
- pascal OSErr DoAEOpenApplication(AppleEvent *, AppleEvent *,long);
- pascal OSErr DoAEOpenDocuments(AppleEvent *, AppleEvent *,long);
- pascal OSErr DoAEPrintDocuments(AppleEvent *, AppleEvent *,long);
- pascal OSErr DoAEQuitApplication(AppleEvent *, AppleEvent *,long);
- void InitAppleEventsStuff(void);
- void DoHighLevelEvent(EventRecord *);
- void RunInitCode(FSSpecPtr);
-
-
- // globals
-
- Boolean gAppleEventsFlag, gQuitFlag;
- long gSleepVal;
-
- // RunInitCode loads and executes all INIT resource code
- // in the file
-
- pascal void DoJSR(ProcPtr p)
- = { 0x205F, 0x4E90 }; // MOVEA.L (A7)+, A0; JSR (A0)
-
- void RunInitCode(FSSpecPtr fileFSSpecPtr)
- {
- OSErr retCode;
- short resRefNum;
- short index;
- Handle initHandle;
-
- resRefNum = FSpOpenResFile(fileFSSpecPtr, fsRdPerm);
- if (resRefNum != -1) {
- for (index = 1; index <= CountResources('INIT'); index++) {
- initHandle = Get1IndResource('INIT', index);
- if (initHandle != nil) {
- HLock(initHandle);
- DoJSR((ProcPtr) *initHandle);
- }
- }
- CloseResFile(resRefNum);
- }
- }
-
- // Apple event handlers to be installed
-
- pascal OSErr DoAEOpenApplication(AppleEvent * theAppleEvent,
- AppleEvent * replyAppleEvent,
- long refCon)
- {
- #pragma unused (theAppleEvent, replyAppleEvent, refCon)
- gQuitFlag = true;
- return noErr;
- }
-
- pascal OSErr DoAEOpenDocuments(AppleEvent * theAppleEvent,
- AppleEvent * replyAppleEvent,
- long refCon)
- {
- #pragma unused (replyAppleEvent, refCon)
-
- OSErr retCode;
- AEDescList docDescList;
- long docCount, index;
- AEKeyword keyword;
- DescType returnedType;
- FSSpec fileFSSpec;
- Size actualSize;
-
- retCode = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList,
- &docDescList);
- if (retCode == noErr) {
-
- if (AECountItems(&docDescList, &docCount) == noErr) {
-
- for (index = 1; index <= docCount; index++) {
- retCode = AEGetNthPtr(&docDescList, index, typeFSS,
- &keyword, &returnedType, (Ptr) &fileFSSpec,
- sizeof(FSSpec), &actualSize);
- if (retCode == noErr) {
- RunInitCode(&fileFSSpec);
- }
- }
- }
- (void) AEDisposeDesc(&docDescList);
- }
-
- gQuitFlag = true;
- return noErr;
- }
-
- pascal OSErr DoAEPrintDocuments(AppleEvent * theAppleEvent,
- AppleEvent * replyAppleEvent,
- long refCon)
- {
- #pragma unused (theAppleEvent, replyAppleEvent, refCon)
- gQuitFlag = true;
- return errAEEventNotHandled;
- }
-
- pascal OSErr DoAEQuitApplication(AppleEvent * theAppleEvent,
- AppleEvent * replyAppleEvent,
- long refCon)
- {
- #pragma unused (theAppleEvent, replyAppleEvent, refCon)
- gQuitFlag = true;
- return noErr;
- }
-
- void InitAppleEventsStuff(void)
- // install Apple event handlers
- {
- OSErr retCode;
-
- retCode = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
- (EventHandlerProcPtr) DoAEOpenApplication, 0, false);
-
- if (retCode == noErr)
- retCode = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
- (EventHandlerProcPtr) DoAEOpenDocuments, 0, false);
-
- if (retCode == noErr)
- retCode = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
- (EventHandlerProcPtr) DoAEPrintDocuments, 0, false);
- if (retCode == noErr)
- retCode = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
- (EventHandlerProcPtr) DoAEQuitApplication, 0, false);
-
- if (retCode != noErr) DebugStr("\pInstall event handler failed");
-
- }
-
- void DoHighLevelEvent(EventRecord * theEventRecPtr)
- // high-level event dispatching
- {
- (void) AEProcessAppleEvent(theEventRecPtr);
- }
-
- main()
- {
- OSErr retCode;
- long gestResponse;
-
- EventRecord mainEventRec;
- Boolean eventFlag;
-
- // initialize QuickDraw globals
-
- InitGraf(&qd.thePort);
-
- // initialize application globals
-
- gQuitFlag = false;
- gSleepVal = 10; // a brief, non-zero sleep time is appropriate
-
- // is the Apple Event Manager available?
- retCode = Gestalt(gestaltAppleEventsAttr, &gestResponse);
- if (retCode != noErr ||
- (gestResponse & (1 << gestaltAppleEventsPresent)) == 0)
-
- gQuitFlag = true; // bail if no Apple Events
-
- // install Apple event handlers
- else
- InitAppleEventsStuff();
-
- // main event loop
-
- while (!gQuitFlag) {
- eventFlag = WaitNextEvent(everyEvent, &mainEventRec, gSleepVal, nil);
-
- if (mainEventRec.what == kHighLevelEvent)
- DoHighLevelEvent(&mainEventRec);
- }
- }
-